/**
 * Created by kayak-lic on 16/11/9.
 */
import React from 'react';
import util from 'util';
import {Switch, Input, Row, Col, Button, Table, Modal, Form, Collapse, Card, Radio, Tabs} from 'antd';
import {SearchDemo, RightButton, ColsEdit, Simeditor} from 'kpl';
import sys_dict from "sys_dict";

const TabPane = Tabs.TabPane;

class TransResultSelect extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            isLoading: false,
            typeName: "fnc_trans_type"
        };
        this.chanpinTypeArray = ["1", "2", "3", "4"];
        this.selectIndex = 0;
        this.searchParams = {
            s_product_type: this.chanpinTypeArray[0],
            "page": 1,
            "rows": sys_dict.defaultPageSize.toString(),
        };
        this.searchParams1 = {
            s_product_type: this.chanpinTypeArray[1],
            "page": 1,
            "rows": sys_dict.defaultPageSize.toString(),
        };
        this.searchParams2 = {
            s_product_type: this.chanpinTypeArray[2],
            "page": 1,
            "rows": sys_dict.defaultPageSize.toString(),
        };
        this.searchParams3 = {
            s_product_type: this.chanpinTypeArray[3],
            "page": 1,
            "rows": sys_dict.defaultPageSize.toString(),
        };
    }

    refresh(paramdic) {
        this.setState({isLoading: true});
        if (paramdic.s_product_type == "1") {
            util.httpRequest({
                type: "POST",
                url: 'translog130401.json',
                plattype: 'app/',
                params: paramdic,
                success: function (result) {
                    console.log("理财", result.rows);
                    //交易金额根据交易状态取不同的值
                    for (var i = 0; i < result.rows.length; i++) {
                        result.rows[i].amt = (result.rows[i].trans_status === "0" || result.rows[i].trans_status === "1" || result.rows[i].trans_status === "2" ? result.rows[i].app_amt : result.rows[i].ack_amt);
                    }
                    console.log(result.rows);
                    this.setState({
                        tblSource: result.rows,
                        totalCount: result.total,
                        isLoading: false,
                    });
                }.bind(this)
            });
        } else if (paramdic.s_product_type == "2") {
            util.httpRequest({
                type: "POST",
                url: 'translog130401.json',
                plattype: 'app/',
                params: paramdic,
                success: function (result) {
                    this.setState({
                        tblSource1: result.rows,
                        totalCount1: result.total,
                    });
                }.bind(this)
            });
        } else if (paramdic.s_product_type == "3") {
            util.httpRequest({
                type: "POST",
                url: 'translog130401.json',
                plattype: 'app/',
                params: paramdic,
                success: function (result) {
                    this.setState({
                        tblSource2: result.rows,
                        totalCount2: result.total,
                    });
                }.bind(this)
            });
        } else if (paramdic.s_product_type == "4") {
            util.httpRequest({
                type: "POST",
                url: 'translog130401.json',
                plattype: 'app/',
                params: paramdic,
                success: function (result) {
                    this.setState({
                        tblSource3: result.rows,
                        totalCount3: result.total,
                    });
                }.bind(this)
            });
        }
    }

    search(params) {
        params.start_date = util.date2str(params.start_date);
        params.end_date = util.date2str(params.end_date);
        params.s_product_type = this.chanpinTypeArray[this.selectIndex],
            params.page = "1";
        params.rows = sys_dict.defaultPageSize.toString();
        this.refresh(params);
    }

    onShowSizeChange(current, pageSize) {
        let params = this.searchParams;
        if (this.selectIndex == 1) {
            params = this.searchParams1;
        } else if (this.selectIndex == 2) {
            params = this.searchParams2;
        } else if (this.selectIndex == 3) {
            params = this.searchParams3;
        }
        params.s_product_type = this.chanpinTypeArray[this.selectIndex],
            params.rows = pageSize;
        params.page = current;
        this.refresh(params);
    }

    onChange(page) {
        let params = this.searchParams;
        if (this.selectIndex == 1) {
            params = this.searchParams1;
        } else if (this.selectIndex == 2) {
            params = this.searchParams2;
        } else if (this.selectIndex == 3) {
            params = this.searchParams3;
        }
        params.s_product_type = this.chanpinTypeArray[this.selectIndex],
            params.page = page;
        this.refresh(params);
    }

    componentDidMount() {
        this.refresh(this.searchParams);
        this.refresh(this.searchParams1);
        this.refresh(this.searchParams2);
        this.refresh(this.searchParams3);
    }

    clickTab(key) {
        this.selectIndex = key;
        console.log("this.selectIndex", this.selectIndex);
        var str;
        switch (this.selectIndex) {
            case "0" :
                str = "fnc_trans_type";
                break;
            case "1" :
                str = "znckcurr_trans_type";
                break;
            case "2" :
                str = "zncktime_trans_type";
                break;
            case "3" :
                str = "gjs_trans_type";
                break;

            default :

        }

        console.log(str)
        this.setState({typeName: str})
    }

    config() {
        const pagination = {
            total: this.state.totalCount,
            showSizeChanger: true,
            showQuickJumper: true,
            isLoading: false,
            pageSizeOptions: sys_dict.pageSizeOptions,
            defaultPageSize: sys_dict.defaultPageSize,
            onShowSizeChange: this.onShowSizeChange.bind(this),
            onChange: this.onChange.bind(this),
        };
        const pagination1 = {
            total: this.state.totalCount1,
            showSizeChanger: true,
            showQuickJumper: true,
            isLoading: false,
            pageSizeOptions: sys_dict.pageSizeOptions,
            defaultPageSize: sys_dict.defaultPageSize,
            onShowSizeChange: this.onShowSizeChange.bind(this),
            onChange: this.onChange.bind(this),
        };
        const pagination2 = {
            total: this.state.totalCount2,
            showSizeChanger: true,
            showQuickJumper: true,
            isLoading: false,
            pageSizeOptions: sys_dict.pageSizeOptions,
            defaultPageSize: sys_dict.defaultPageSize,
            onShowSizeChange: this.onShowSizeChange.bind(this),
            onChange: this.onChange.bind(this),
        };
        const pagination3 = {
            total: this.state.totalCount3,
            showSizeChanger: true,
            showQuickJumper: true,
            isLoading: false,
            pageSizeOptions: sys_dict.pageSizeOptions,
            defaultPageSize: sys_dict.defaultPageSize,
            onShowSizeChange: this.onShowSizeChange.bind(this),
            onChange: this.onChange.bind(this),
        };

        return {
            tableConfig: {
                pagination: pagination,
                dataSource: this.state.tblSource,
                columns: [
                    {
                        title: '流水号',
                        dataIndex: 'trans_serno',
                        key: 'trans_serno',
                    }, {
                        title: '交易日期时间',
                        dataIndex: 'trans_date',
                        key: 'fnc_trans_type',
                    }, {
                        title: '交易类型',
                        dataIndex: 'busi_code',
                        key: 'busi_code',
                        render: function (value) {
                            var str = util.convert("fnc_trans_type", value);
                            return str;
                        }
                    }, {
                        title: '客户姓名',
                        dataIndex: 'cust_name',
                        key: 'cust_name',
                    }, {
                        title: '交易金额',
                        dataIndex: 'amt',
                        key: 'amt'
                    }, {
                        title: '交易渠道',
                        dataIndex: 'channel_type',
                        key: 'channel_type',
                        render: function (value, data, rowIndex) {
                            var str = util.convert("channel_type", value);
                            return str;
                        }
                    }, {
                        title: '银行卡号',
                        dataIndex: 'card_no',
                        key: 'card_no',
                    }, {
                        title: '交易状态',
                        dataIndex: 'trans_status',
                        key: 'trans_status',
                        render: function (value, data, rowIndex) {
                            var str = util.convert("fnc_trans_status", value);
                            return str;
                        }
                    }]
            },
            tableConfig1: {
                pagination: pagination1,
                dataSource: this.state.tblSource1,
                columns: [
                    {
                        title: '流水号',
                        dataIndex: 'trans_serno',
                        key: 'trans_serno',
                    }, {
                        title: '交易日期时间',
                        dataIndex: 'trans_date',
                        key: 'trans_date',
                    }, {
                        title: '交易类型',
                        dataIndex: 'busi_code',
                        key: 'busi_code',
                        render: function (value) {
                            var str = util.convert("znckcurr_trans_type", value);
                            return str;
                        }
                    }, {
                        title: '客户姓名',
                        dataIndex: 'cust_name',
                        key: 'cust_name',
                    }, {
                        title: '交易金额',
                        dataIndex: 'trans_money',
                        key: 'trans_money',
                    }, {
                        title: '交易渠道',
                        dataIndex: 'channel_type',
                        key: 'channel_type',
                        render: function (value, data, rowIndex) {
                            var str = util.convert("channel_type", value);
                            return str;
                        }
                    }, {
                        title: '银行卡号',
                        dataIndex: 'card_no',
                        key: 'card_no',
                    }, {
                        title: '交易状态',
                        dataIndex: 'trans_status',
                        key: 'trans_status',
                        render: function (value, data, rowIndex) {
                            var str = util.convert("znckcurr_trans_status", value);
                            return str;
                        }
                    }]
            },
            tableConfig2: {
                pagination: pagination2,
                dataSource: this.state.tblSource2,
                columns: [
                    {
                        title: '流水号',
                        dataIndex: 'trans_serno',
                        key: 'trans_serno',
                    }, {
                        title: '交易日期时间',
                        dataIndex: 'trans_date',
                        key: 'trans_date',
                    }, {
                        title: '交易类型',
                        dataIndex: 'busi_code',
                        key: 'busi_code',
                        render: function (value) {
                            var str = util.convert("zncktime_trans_type", value);
                            return str;
                        }
                    }, {
                        title: '客户姓名',
                        dataIndex: 'cust_name',
                        key: 'cust_name',
                    }, {
                        title: '交易金额',
                        dataIndex: 'trans_amt',
                        key: 'trans_amt',
                    }, {
                        title: '交易渠道',
                        dataIndex: 'channel_type',
                        key: 'channel_type',
                        render: function (value, data, rowIndex) {
                            var str = util.convert("channel_type", value);
                            return str;
                        }
                    }, {
                        title: '银行卡号',
                        dataIndex: 'card_no',
                        key: 'card_no',
                    }, {
                        title: '交易状态',
                        dataIndex: 'trans_status',
                        key: 'trans_status',
                        render: function (value, data, rowIndex) {
                            var str = util.convert("zncktime_trans_status", value);
                            return str;
                        }
                    }]
            },
            tableConfig3: {
                pagination: pagination3,
                dataSource: this.state.tblSource3,
                columns: [
                    {
                        title: '流水号',
                        dataIndex: 'trans_serno',
                        key: 'trans_serno',
                    }, {
                        title: '交易日期时间',
                        dataIndex: 'trans_date',
                        key: 'trans_date',
                    }, {
                        title: '交易类型',
                        dataIndex: 'busi_code',
                        key: 'busi_code',
                        render: function (value) {
                            var str = util.convert("gjs_trans_type", value);
                            return str;
                        }
                    }, {
                        title: '客户姓名',
                        dataIndex: 'invoice',
                        key: 'invoice',
                    }, {
                        title: '交易金额',
                        dataIndex: 'total_amt',
                        key: 'total_amt',
                    }, {
                        title: '交易渠道',
                        dataIndex: 'channel_id',
                        key: 'channel_id',
                        render: function (value, data, rowIndex) {
                            // asdasds


                            var str = util.convert("channel_type", value);
                            return str;
                        }
                    }, {
                        title: '银行卡号',
                        dataIndex: 'card_no',
                        key: 'card_no',
                    }, {
                        title: '交易状态',
                        dataIndex: 'trans_status',
                        key: 'trans_status',
                        render: function (value, data, rowIndex) {
                            var str = util.convert("gjs_trans_status", value);
                            return str;
                        }
                    }]
            },
        }
    }

    render() {

        let config = this.config();
        const searchConfig = {
            searchTitle: "查询条件",
            searchInit: {
                trans_serno: "",
                cust_id: "",
                cust_name: "",
                acct_no: "",
                trans_type: "",
                start_date: "",
                end_date: ""
            },
            searchField: [
                [
                    {propsname: "流水号", propsKey: "trans_serno", propsType: "input", key: "111"},
                    {propsname: "客户姓名", propsKey: "cust_name", propsType: "input", key: "113"},
                    {propsname: "银行卡号 ", propsKey: "card_no", propsType: "input", key: "114"},
                ],
                [

                    {propsname: "交易类型", propsKey: this.state.typeName, propsType: "select", key: "115"},
                    {propsname: "交易时间", propsKey: "start_date", propsType: "datepicker", key: "116"},
                    {propsname: "至", propsKey: "end_date", propsType: "datepicker", key: "117"},
                ],
                []
            ]
        };
        return (
            <Tabs defaultActiveKey="0" onChange={this.clickTab.bind(this)}>
                <TabPane tab="理财" key="0">
                    <SearchDemo key="s0" search={this.search.bind(this)} searchConfig={searchConfig}></SearchDemo>
                    <Table key="t0" {...config.tableConfig}></Table>
                </TabPane>
                <TabPane tab="智能存活期" key="1">
                    <SearchDemo key="s1" search={this.search.bind(this)} searchConfig={searchConfig}></SearchDemo>
                    <Table key="t1" {...config.tableConfig1}></Table>
                </TabPane>
                <TabPane tab="智能存定期" key="2">
                    <SearchDemo key="s2" search={this.search.bind(this)} searchConfig={searchConfig}></SearchDemo>
                    <Table key="t2" {...config.tableConfig2}></Table>
                </TabPane>
                <TabPane tab="贵金属" key="3">
                    <SearchDemo key="s3" search={this.search.bind(this)} searchConfig={searchConfig}></SearchDemo>
                    <Table key="t3" {...config.tableConfig3}></Table>
                </TabPane>
            </Tabs>
        );
    }
}

export default TransResultSelect;